package com.xy.yunduo.utils.date;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

public final class DateTimeUtil {

    private DateTimeUtil() {
    }

    public static String format(DateTime dt, String pattern) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
        return formatter.print(dt);
    }

    public static DateTime parse(String date, String pattern) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
        return formatter.parseDateTime(date);
    }

    public static Date to(String source, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = simpleDateFormat.parse(source);
        } catch (Exception e) {
        }
        return date;
    }

    public static String formatDate(DateTime dt) {
        return format(dt, "yyyy-MM-dd");
    }

    public static DateTime parseDate(String date) {
        return parse(date, "yyyy-MM-dd");
    }

    public static Date toDate(String date) {

        return to(date, "yyyy-MM-dd");
    }

    public static String formatCompactDate(DateTime dt) {
        return format(dt, "yyyyMMdd");
    }

    public static DateTime parseCompactDate(String date) {
        return parse(date, "yyyyMMdd");
    }

    public static String formatTime(DateTime dt) {
        return format(dt, "HH:mm:ss");
    }

    public static String formatCompactTime(DateTime dt) {
        return format(dt, "HHmmss");
    }

    public static String formatDateTime(DateTime dt) {
        return format(dt, "yyyy-MM-dd HH:mm:ss");
    }

    public static DateTime parseDateTime(String date) {
        return parse(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatCompactDateTime(DateTime dt) {
        return format(dt, "yyyyMMddHHmmss");
    }

    public static DateTime parseCompactDateTime(String date) {
        return parse(date, "yyyyMMddHHmmss");
    }

    /**
     * 获取精确到秒的时间戳
     * @param date
     * @return
     */
    public static Long getSecondTimestampTwo(Date date){
        if (null == date) {
            return 0L;
        }
        String timestamp = String.valueOf(date.getTime()/1000);
        return Long.valueOf(timestamp);
    }

    public static String formatHumanReadable(Date date) {
        if (date == null) {
            return "";
        }

        return formatHumanReadable(new DateTime(date));
    }

    public static String formatHumanReadable(DateTime date) {
        if (date == null) {
            return "";
        }

        long currentDate = System.currentTimeMillis();
        long createDate = date.getMillis();
        long sub = currentDate - createDate;
        String val = "刚刚";
        sub = sub / 1000;
        if (sub > 0) {
            if (sub < 60) {
                val = sub + "秒前";
            } else if (sub >= 60 && sub < 60 * 60) {
                val = sub / 60 + "分钟前";
            } else if (sub >= 60 * 60 && sub < 60 * 60 * 24) {
                val = (sub / (60 * 60)) + "小时前";
            } else if (sub >= 60 * 60 * 24 && sub < 60 * 60 * 24 * 365) {
                val = (sub / (60 * 60 * 24)) + "天前";
            } else {
                val = "1年前";
            }
        }

        return val;
    }

    /**
     * 获取当前年月
     * @return
     */
    public static String getCurrentYearMonth(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        String xAsisName = format.format(date);
        return xAsisName;
    }

    /**
     * 获取去年年月
     * @return
     */
    public static String getYesterYearMonth(){
        return getYesterYearMonth(1,2);
    }
    /**
     * 获取上个月
     * @return
     */
    public static String getTopMonth(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date y = c.getTime();
        String date = format.format(y);
        return  date;
    }
    /**
     * 获取前N个月
     * @return
     */
    public static String getTopMonth(String currDate,int fMonth) throws Exception{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(format.parse(currDate));
        c.add(Calendar.MONTH, -fMonth);
        Date y = c.getTime();
        String date = format.format(y);
        return  date;
    }
    /**
     * 获取去年年月
     * @return
     */
    public static String getYesterYearMonth(int fYear,int fMonth){
        Calendar ca = Calendar.getInstance();
        int year=ca.get(Calendar.YEAR);
        int month=ca.get(Calendar.MONTH);

        Calendar c = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        c.setTime(DateTimeUtil.toDate((year - fYear) + "-" + month + "-01"));
        c.add(Calendar.MONTH, fMonth);
        Date date = c.getTime();
        String xAsisName = format.format(date);
        return xAsisName;
    }

    public static String replaceDate(String date){
        if(date==null||date.length()==0)
            return "";
        return date.replace("-","");
    }

    public static String getTopYearDate(String date){
        if(date==null||date.length()==0)
            return "";
        String arr[]=date.split("-");
        Integer year=Integer.parseInt(arr[0]);
        StringBuilder str=new StringBuilder();
        str.append(year-1).append(arr[1]);
        return str.toString();
    }

    public static String getTopDate(int top){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.YEAR, -top);
        Date y = c.getTime();
        String year = format.format(y);
        return  year;
    }
    public static String getTopYear(int top){
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.YEAR, -top);
        Date y = c.getTime();
        String year = format.format(y);
        return  year;
    }

    public static Map<String,String> getDateList(String startDate, String endDate) throws  Exception{
        Map<String,String> result = new LinkedHashMap<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date sDate = format.parse(startDate);
        Date eDate = format.parse(endDate);

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(sDate);

        while(sDate.getTime()<=eDate.getTime()){
            String rq=format.format(tempStart.getTime());
            result.put(rq,rq);
            tempStart.add(Calendar.MONTH, 1);
            sDate = tempStart.getTime();
        }
        return result;
    }

    public static Integer getDateDiff(String startDate, String endDate) throws ParseException {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar bef = Calendar.getInstance();
            Calendar aft = Calendar.getInstance();
            bef.setTime(sdf.parse(startDate));
            aft.setTime(sdf.parse(endDate));
            int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
            int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
            return month+result;
        }catch (ParseException e){
            throw e;
        }
    }

    public static void main(String[] args) {
        try {
            Map<String,String> v= getDateList("201711","201805");
            int c=0;
        }catch (Exception e){

        }
        int ii = 0;
    }
}
